const formatTime = date => {
    console.log(date);
    date = new Date(date)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : `0${n}`
}

const arrayToTree = (items) => {
    const result = [];   // 存放结果集
    const itemMap = {};  // 
    for (const item of items) {
        const id = item.id;
        const parentid = item.parentId;

        itemMap[id] = {
            ...item,
            children: itemMap[id] && itemMap[id]['children'] ? itemMap[id]['children'] : []
        }

        const treeItem = itemMap[id];

        if (parentid === "0") {
            result.push(treeItem);
        } else {
            !itemMap[parentid] && (itemMap[parentid] = { children: [] })
            itemMap[parentid].children.push(treeItem)
        }

    }
    return result;
}
const arrayToMap = (items) => {
    const itemMap = {};  // 
    for (const item of items) {
        const id = item.id;
        const parentid = item.parentId;
        itemMap[id] = {
            ...item,
            children: itemMap[id] && itemMap[id]['children'] ? itemMap[id]['children'] : []
        }
        const treeItem = itemMap[id];

        if (parentid !== "0" && (itemMap[parentid] && itemMap[parentid].value ? itemMap[parentid].value[0] == 'D' : true)) {
            !itemMap[parentid] && (itemMap[parentid] = { children: [] })
            itemMap[parentid].children.push(treeItem)
        }
    }
    return itemMap;
}


module.exports = {
    formatTime,
    arrayToTree,
    arrayToMap
}
